package com.spc.car.web.controller.sys;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spc.car.core.framework.web.domain.AjaxResult;
import com.spc.car.data.domain.sys.po.SysCalendarCategoryPO;
import com.spc.car.data.entity.sys.SysCalendarCategory;
import com.spc.car.data.service.sys.SysCalendarCategoryService;
import com.spc.car.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  日历类型 前端控制器
 * </p>
 *
 * @author 
 * @since 2020-04-01
 */
@Api(tags = "日历类型")
@Validated
@RestController
@RequestMapping("/calendarCategory")
public class SysCalendarCategoryController extends BaseController {

    @Autowired
    private SysCalendarCategoryService sysCalendarCategoryService;

    @ApiOperation(value = "添加日历类型" , notes = "添加日历类型")
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@Valid @RequestBody List<SysCalendarCategoryPO > sysCalendarCategoryPOList){

        List<SysCalendarCategory>  sysCalendarCategoryList = new ArrayList<>();

        for (SysCalendarCategoryPO po : sysCalendarCategoryPOList) {

            SysCalendarCategory sysCalendarCategory = new SysCalendarCategory();
            BeanUtils.copyProperties(po , sysCalendarCategory);
            sysCalendarCategory.setCreatorId(getPrintUser().getUserId());
            sysCalendarCategoryList.add(sysCalendarCategory);
        }

        boolean result = sysCalendarCategoryService.saveOrUpdateBatch(sysCalendarCategoryList);
        return result ? AjaxResult.success("添加日历类型成功") : AjaxResult.error("添加日历类型失败");
    }

    @ApiOperation(value = "个人日历类型列表" , notes = "个人日历类型列表")
    @PostMapping("/listCalendarCategory")
    public AjaxResult listCalendarCategory(){
        QueryWrapper<SysCalendarCategory> condition = new QueryWrapper<>();
        condition.eq("creator_id" , getPrintUser().getUserId())
                .eq("category", "U")
                .orderByDesc("create_time");
        List<SysCalendarCategory> calendarCategoryList = sysCalendarCategoryService.list(condition);
        return  AjaxResult.success(calendarCategoryList);
    }

    @ApiOperation(value = "删除日历类型" , notes = "删除日历类型")
    @PostMapping("/remove")
    public AjaxResult remove(@NotNull Integer id){
        boolean result = sysCalendarCategoryService.removeById(id);
        return  result ? AjaxResult.success("删除日历类型成功") : AjaxResult.error("删除日历类型失败");
    }

}

